<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="140px">
    <el-form-item label="优惠卷名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="优惠卷名称"></el-input>
    </el-form-item>
    <el-form-item label="金额" prop="typeMoney">
      <el-input-number v-model="dataForm.typeMoney" controls-position="right"  :min="1" ></el-input-number>
    </el-form-item>
    <el-form-item label="发放方式" prop="sendType">
      <el-radio v-model="dataForm.sendType" label="0">按订单发放</el-radio>
      <el-radio v-model="dataForm.sendType" label="1">按用户发放</el-radio>
      <el-radio v-model="dataForm.sendType" label="2">商品转发送券</el-radio>
      <el-radio v-model="dataForm.sendType" label="3">按商品发放</el-radio>
      <el-radio v-model="dataForm.sendType" label="4">新用户注册</el-radio>
      <el-radio v-model="dataForm.sendType" label="7">包邮优惠</el-radio>
    </el-form-item>
    <el-form-item label="最小金额" prop="minAmount">
      <el-input-number v-model="dataForm.minAmount" controls-position="right"  :min="1" ></el-input-number>
    </el-form-item>
    <el-form-item label="最大金额" prop="maxAmount">
      <el-input-number v-model="dataForm.maxAmount" controls-position="right"  :min="1" ></el-input-number>
    </el-form-item>
    <el-form-item label="发放开始时间" prop="sendStartDate">
      <el-date-picker
        v-model="dataForm.sendStartDate"
        type="date"
        placeholder="选择日期">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="发放结束时间" prop="sendEndDate">
      <el-date-picker
        v-model="dataForm.sendEndDate"
        type="date"
        placeholder="选择日期">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="使用开始时间" prop="useStartDate">
      <el-date-picker
        v-model="dataForm.useStartDate"
        type="date"
        placeholder="选择日期">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="使用结束时间" prop="useEndDate">
      <el-date-picker
        v-model="dataForm.useEndDate"
        type="date"
        placeholder="选择日期">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="最小商品金额" prop="minGoodsAmount">
      <el-input-number v-model="dataForm.minGoodsAmount" controls-position="right"  :min="1" ></el-input-number>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          typeMoney: '',
          sendType: '0',
          minAmount: '',
          maxAmount: '',
          sendStartDate: '',
          sendEndDate: '',
          useStartDate: '',
          useEndDate: '',
          minGoodsAmount: '',
          minTransmitNum: ''
        },
        dataRule: {
          name: [
            { required: true, message: '优惠卷名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/platform/coupon/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.data.name
                this.dataForm.typeMoney = data.data.typeMoney
                this.dataForm.sendType = data.data.sendType + ''
                this.dataForm.minAmount = data.data.minAmount
                this.dataForm.maxAmount = data.data.maxAmount
                this.dataForm.sendStartDate = data.data.sendStartDate
                this.dataForm.sendEndDate = data.data.sendEndDate
                this.dataForm.useStartDate = data.data.useStartDate
                this.dataForm.useEndDate = data.data.useEndDate
                this.dataForm.minGoodsAmount = data.data.minGoodsAmount
                this.dataForm.minTransmitNum = data.data.minTransmitNum
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/platform/coupon/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'typeMoney': this.dataForm.typeMoney,
                'sendType': this.dataForm.sendType,
                'minAmount': this.dataForm.minAmount,
                'maxAmount': this.dataForm.maxAmount,
                'sendStartDate': this.dataForm.sendStartDate,
                'sendEndDate': this.dataForm.sendEndDate,
                'useStartDate': this.dataForm.useStartDate,
                'useEndDate': this.dataForm.useEndDate,
                'minGoodsAmount': this.dataForm.minGoodsAmount,
                'minTransmitNum': this.dataForm.minTransmitNum
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
